using System;
using System.Collections.Generic;
using System.Text;

namespace Colman.Rpg.Server
{
    /// <summary>
    /// This cache holds all server's connected players
    /// </summary>
    public static class PlayersCache
    {
        /// <summary>
        /// Holds connected players data
        /// </summary>
        private static Dictionary<string, Player> players = new Dictionary<string, Player>();

        /// <summary>
        /// Gets requested player data
        /// </summary>
        /// <param name="username">requested user name</param>
        /// <returns>requested player object</returns>
        public static Player Get(string username)
        {
            return players[username];
        }

        /// <summary>
        /// Sets/Updates new player data
        /// </summary>
        /// <param name="player">Player;s data</param>
        public static void Set(Player player)
        {
            players[player.Name] = player;
            
            // Updates the DB
            Dal.UpdatePlayer(player);
        }

        /// <summary>
        /// Gets all players data
        /// </summary>
        /// <returns>Players objects</returns>
        public static Dictionary<string, Player> GetAllPlayers()
        {
            return players;
        }

        /// <summary>
        /// Registers new player data to server
        /// </summary>
        /// <param name="player">Player's data</param>
        public static void RegisterUser(Player player)
        {
            players.Add(player.Name, player);
        }

        /// <summary>
        /// UnRegsisters requested player from the server
        /// </summary>
        /// <param name="username">player to unregister</param>
        public static void UnregisterUser(string username)
        {
            players.Remove(username);
        }
    }
}
